/**
 * 个人资料模块类型定义
 * 基于后端API文档：我的模块API.md
 */

// ==================== 基础类型 ====================

/**
 * 用户基本信息响应
 */
export interface ProfileInfoRespVO {
  id: number
  username: string
  nickname: string
  avatar: string
  email?: string
  phone?: string
  age?: number
  createTime: string
}

/**
 * 用户详细信息响应
 */
export interface ProfileDetailRespVO {
  id: number
  username: string
  nickname: string
  avatar: string
  email?: string
  phone?: string
  age?: number
  createTime: string
  updateTime: string
}

/**
 * 用户信息更新请求
 */
export interface ProfileUpdateReqVO {
  avatar?: string
  nickname: string
  // phone?: string // 注释掉手机号字段
  email?: string
  age?: number
}

/**
 * 头像上传响应
 */
export interface ProfileAvatarRespVO {
  avatarUrl: string
}

/**
 * 用户统计概览响应
 */
export interface UserStatsOverviewRespVO {
  totalPunchDays: number
  totalReadBooks: number
  currentStreak: number
  maxStreak: number
  totalFavorites: number
  totalRecordings: number
  joinedClasses: number
  completedAssignments: number
}

// ==================== 表单相关类型 ====================

/**
 * 个人资料编辑表单数据
 */
export interface ProfileEditFormData {
  avatar: string
  nickname: string
  // phone?: string // 注释掉手机号字段
  email?: string
  age?: number
}

/**
 * 头像上传表单数据
 */
export interface AvatarUploadFormData {
  avatar: File
}

// ==================== API响应类型 ====================

/**
 * 通用API响应格式
 */
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

/**
 * 获取用户基本信息API响应
 */
export type GetProfileInfoResponse = ApiResponse<ProfileInfoRespVO>

/**
 * 获取用户详细信息API响应
 */
export type GetProfileDetailResponse = ApiResponse<ProfileDetailRespVO>

/**
 * 更新用户信息API响应
 */
export type UpdateProfileResponse = ApiResponse<void>

/**
 * 上传头像API响应
 */
export type UploadAvatarResponse = ApiResponse<ProfileAvatarRespVO>

/**
 * 获取用户统计概览API响应
 */
export type GetUserStatsOverviewResponse = ApiResponse<UserStatsOverviewRespVO>

// ==================== 错误码类型 ====================

/**
 * 个人资料模块错误码
 */
export enum ProfileErrorCode {
  USER_NOT_FOUND = 50001,
  NICKNAME_REQUIRED = 50002,
  PHONE_FORMAT_ERROR = 50003,
  AVATAR_UPLOAD_FAILED = 50004,
}

/**
 * 错误信息映射
 */
export const ProfileErrorMessages: Record<ProfileErrorCode, string> = {
  [ProfileErrorCode.USER_NOT_FOUND]: '用户信息不存在',
  [ProfileErrorCode.NICKNAME_REQUIRED]: '昵称不能为空',
  [ProfileErrorCode.PHONE_FORMAT_ERROR]: '手机号格式错误',
  [ProfileErrorCode.AVATAR_UPLOAD_FAILED]: '头像上传失败',
}

// ==================== 组件Props类型 ====================

/**
 * 用户信息展示组件Props
 */
export interface UserInfoDisplayProps {
  userInfo: ProfileInfoRespVO
  stats?: UserStatsOverviewRespVO
  loading?: boolean
}

/**
 * 个人资料编辑组件Props
 */
export interface ProfileEditProps {
  initialData?: ProfileDetailRespVO
  onSave?: (data: ProfileUpdateReqVO) => void
  onAvatarUpload?: (file: File) => void
  loading?: boolean
}

// ==================== 状态管理类型 ====================

/**
 * 个人资料模块状态
 */
export interface ProfileState {
  userInfo: ProfileInfoRespVO | null
  userStats: UserStatsOverviewRespVO | null
  loading: boolean
  error: string | null
}

/**
 * 个人资料模块Actions
 */
export interface ProfileActions {
  getUserInfo: () => Promise<void>
  getUserStats: () => Promise<void>
  updateUserInfo: (data: ProfileUpdateReqVO) => Promise<void>
  uploadAvatar: (file: File) => Promise<string>
  clearError: () => void
}

// ==================== 工具类型 ====================

/**
 * 头像文件验证规则
 */
export interface AvatarValidationRules {
  maxSize: number // 最大文件大小（字节）
  allowedTypes: string[] // 允许的文件类型
  maxWidth?: number // 最大宽度
  maxHeight?: number // 最大高度
}

/**
 * 默认头像验证规则
 */
export const DEFAULT_AVATAR_RULES: AvatarValidationRules = {
  maxSize: 2 * 1024 * 1024, // 2MB
  allowedTypes: ['image/jpeg', 'image/png', 'image/gif'],
  maxWidth: 800,
  maxHeight: 800,
}

/**
 * 手机号验证正则
 */
export const PHONE_REGEX = /^1[3-9]\d{9}$/

/**
 * 邮箱验证正则
 */
export const EMAIL_REGEX = /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/
